package com.situ.mall.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.mall.common.exception.SituException;
import com.situ.mall.common.util.JwtUtil;
import com.situ.mall.common.util.Result;
import com.situ.mall.pojo.entity.user.User;
import com.situ.mall.user.pojo.dto.UserPasswordDTO;
import com.situ.mall.user.pojo.query.UserQuery;
import com.situ.mall.user.service.IAccountBalanceService;
import com.situ.mall.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sun Huijuan
 * @since 2024-10-14
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserService userService;
	
	@Autowired
	private IAccountBalanceService accountBalanceService;
	
	@RequestMapping("/login")
	public Result<User> login(@RequestBody User user) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("name", user.getName());
		User loginUser = userService.getOne(queryWrapper);
		//判断用户名是否存在
		if (loginUser == null) {
			throw new SituException("用户名不存在！");
		}
		//判断密码是否正确
		if (!loginUser.getPassword().equals(user.getPassword())) {
			throw new SituException("密码错误！");
		}
		if(loginUser.getStatus()==0){
			throw new SituException("该账号被禁用！");
		}
		//构建Token
		/*Map<String,Object> map=new HashMap<>();
		map.put("id",loginUser.getId());
		map.put("name",loginUser.getName());
		String token = JwtUtil.createToken(map);
		
		Map<String,Object> map1=new HashMap<>();
		map1.put("token",token);
		map1.put("user",loginUser);*/
		return Result.ok(loginUser);
	}
	
	@GetMapping("/selectById/{id}")
	public Result<User> selectById(@PathVariable Long id) {
		User user=userService.getById(id);
		return Result.ok(user);
	}
	
	@PostMapping("/insert")
	public Result add(@RequestBody User user) {
		/*QueryWrapper<User> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("name",user.getName());
		User loginUser=userService.getOne(queryWrapper);
		if(loginUser!=null){
			return Result.error("用户名已存在！请重试！");
		}*/
		userService.save(user);
		return Result.ok("注册成功！");
	}
	
	@GetMapping("/findUserName")
	public Result findUserName(User user){
		QueryWrapper<User> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("name",user.getName());
		User loginUser=userService.getOne(queryWrapper);
		if(loginUser!=null){
			return Result.error("用户名已存在！请重试！");
		}
		return Result.ok(user);
	}
	
	@PutMapping("/updateSelf")
	public Result updateSelf(@RequestBody User user) {
		userService.updateById(user);
		return Result.ok("修改成功！");
	}
	
	@PutMapping("/updatePassword")
	public Result updatePassword(@RequestBody UserPasswordDTO userPasswordDTO) {
		User user=userService.getById(userPasswordDTO.getId());
		if(user!=null){
			if(!(user.getPassword()).equals(userPasswordDTO.getOldPassword())){
				return Result.error("原密码错误！");
			}else {
				UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
				updateWrapper.set("password",userPasswordDTO.getNewPassword()).eq("id",userPasswordDTO.getId());
				userService.update(updateWrapper);
				return Result.ok("修改成功！");
			}
		}else {
			return Result.error("用户出错！");
		}
	}
	
	@GetMapping("/list")
	public Result list(UserQuery userQuery) {
		IPage<User> page = new Page<>(userQuery.getPage(), userQuery.getLimit());
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.like(!ObjectUtils.isEmpty(userQuery.getName()), "name", userQuery.getName());
		/*queryWrapper.between(!ObjectUtils.isEmpty(userQuery.getBeginCreateTime())&&!ObjectUtils.isEmpty(userQuery.getEndCreateTime()),
				"create_time",userQuery.getBeginCreateTime(),userQuery.getEndCreateTime());*/
		userService.page(page, queryWrapper);
		Map<String, Object> map = new HashMap<>();
		map.put("total", page.getTotal());
		map.put("list", page.getRecords());
		return Result.ok(map);
	}
	
	@PutMapping("/updateStatus/{id}/{status}")
	public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status){
		UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
		updateWrapper.eq("id",id).set("status",status);
		userService.update(updateWrapper);
		if(status==0){
			return Result.ok("该账号停用成功！");
		}else {
			return Result.ok("该账号解封成功！");
		}
	}
	
	@GetMapping("/selectUserCount")
	public Result selectUserCount(){
		return Result.ok(userService.count());
	}
	
	@PutMapping("/update")
	public Result update(@RequestBody User user){
		userService.updateById(user);
		return Result.ok("修改成功！");
	}
}

